package com.thtf.openguess.service.impl;

import com.google.common.base.Stopwatch;
import com.thtf.openguess.aop.annotation.Time;
import com.thtf.openguess.openguess.C201600000020111;
import com.thtf.openguess.mapper.C201600000020111Mapper;
import com.thtf.openguess.openguess.C201600000020111;
import com.thtf.openguess.service.AsyncService;
import com.thtf.openguess.service.C201600000020111Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thtf.openguess.util.Util;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import uk.co.jemos.podam.api.PodamFactory;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.CountDownLatch;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author auth
 * @since 2022-05-27
 */
@Service
public class C201600000020111ServiceImpl extends ServiceImpl<C201600000020111Mapper, C201600000020111> implements C201600000020111Service {

    @Resource
    private AsyncService asyncService;
    @Override
    @Time
    public String insertLonely(Integer num) {
        for (int i=0 ; i<num ; i ++){
            C201600000020111 c201600000020111 = Util.forEachSetValue(new C201600000020111());
            this.save(c201600000020111);
        }
        return null;
    }

    @Override
    @Time
    public String insertBatch(Integer num) {
        List<C201600000020111> list = new ArrayList<>();
        long start = System.currentTimeMillis();
        for (int i=0 ; i<num ; i ++){
            C201600000020111 c201600000020111 = new C201600000020111();
            Util.forEachSetValue(c201600000020111);
//            C201600000020111 c201600000020111 = Util.forEachSetValue(new C201600000020111());
            list.add(c201600000020111);
        }
        long end = System.currentTimeMillis();
        System.out.println("装载数据耗费时间:"+ (end - start) / 1000 + " s");

        long start1 = System.currentTimeMillis();
        this.saveBatch(list);
        long end1 = System.currentTimeMillis();
        System.out.println("批量新增耗费时间:"+ (end1 - start1) / 1000 + " s");
        return null;
    }

    @Override
    @Time
    public String insertThread(Integer sqlNum, Integer threadNum) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(threadNum);
        Stopwatch started = Stopwatch.createStarted();
        for (int i = 1; i <= threadNum; i++) {
            // 第j次提交步长
            List<C201600000020111> list= new ArrayList<>();
            for (int j = 1; j <= sqlNum; j++) {
                C201600000020111 c201600000020111 = Util.forEachSetValue(new C201600000020111());
                list.add(c201600000020111);
            }
            // 添加执行SQL
            asyncService.dealAdd(list, countDownLatch, sqlNum,this);

        }
        countDownLatch.await();
        return null;
    }

    @Override
    @Time
    public String insertZzjgdm(Integer sqlNum,String zzjgdm) {
        List<C201600000020111> list = new ArrayList<>();
        for (int i=0 ; i<sqlNum ; i ++){
            C201600000020111 c201600000020111 = Util.forEachSetValue(new C201600000020111());
            c201600000020111.setZzjgdm(zzjgdm);
            list.add(c201600000020111);
        }
        this.saveBatch(list);
        return null;
    }

    @Override
    @Time
    public String updateSingle(String zzjgdm, Integer type) {
        Collection<C201600000020111> c201600000020111s = this.selectList(zzjgdm);
        c201600000020111s.forEach(c201600000020111 -> {
            C201600000020111 c201600000020111A=new C201600000020111();
            c201600000020111A.setBgq(c201600000020111.getBgq());
            switch (type){
                case 1: this.addLess(c201600000020111A); break;
                case 2: this.addMore(c201600000020111A); break;
                case 3: this.addAll(c201600000020111A); break;
            }
            this.updateById(c201600000020111A);
        });

        return null;
    }

    @Override
    @Time
    public String updatebatch(String zzjgdm, Integer type) {
        Collection<C201600000020111> c201600000020111s = this.selectList(zzjgdm);
        List<C201600000020111> list = new ArrayList<>();
        c201600000020111s.forEach(c201600000020111 -> {
            C201600000020111 c201600000020111A=new C201600000020111();
            c201600000020111A.setBgq(c201600000020111.getBgq());
            switch (type){
                case 1: this.addLess(c201600000020111A); break;
                case 2: this.addMore(c201600000020111A); break;
                case 3: this.addAll(c201600000020111A); break;
            }
            list.add(c201600000020111A);
        });
        this.updateBatchById(list);
        return null;
    }

    @Override
    @Time
    public String deleteSingle(String zzjgdm) {
        Collection<C201600000020111> c201600000020111s = this.selectList(zzjgdm);
        c201600000020111s.forEach(c201600000020111 -> {
            this.removeById(c201600000020111.getBgq());
        });
        return null;
    }

    @Override
    @Time
    public String deleteBatch(String zzjgdm) {
        Collection<C201600000020111> c201600000020111s = this.selectList(zzjgdm);
        List<String> list=new ArrayList<>();
        c201600000020111s.forEach(c201600000020111 -> {
            list.add(c201600000020111.getBgq());
        });
        this.removeByIds(list);
        return null;
    }

    private void addLess(C201600000020111 c201600000020111A) {
        c201600000020111A.setA001(Util.ramdom().toString());
        c201600000020111A.setA002(Util.ramdom().toString());
    }

    private void addMore(C201600000020111 c201600000020111A) {
        this.addLess(c201600000020111A);
        c201600000020111A.setA002(Util.ramdom().toString());
        c201600000020111A.setA003(Util.ramdom().toString());
        c201600000020111A.setA004(Util.ramdom().toString());
        c201600000020111A.setA005(Util.ramdom().toString());
        c201600000020111A.setA006(Util.ramdom().toString());
        c201600000020111A.setB94(Util.ramdom().toString());
        c201600000020111A.setB95(Util.ramdom().toString());
        c201600000020111A.setB96(Util.ramdom().toString());
        c201600000020111A.setB97(Util.ramdom().toString());
        c201600000020111A.setB98(Util.ramdom().toString());
        c201600000020111A.setB99(Util.ramdom().toString());
        c201600000020111A.setB100(Util.ramdom().toString());
        c201600000020111A.setB101(Util.ramdom().toString());
        c201600000020111A.setB103b(Util.ramdom().toString());
        c201600000020111A.setB1031b(Util.ramdom().toString());
        c201600000020111A.setB1032b(Util.ramdom().toString());
        c201600000020111A.setB1033b(Util.ramdom().toString());
        c201600000020111A.setB104(Util.ramdom().toString());
        c201600000020111A.setB191(Util.ramdom().toString());
        c201600000020111A.setB1921(Util.ramdom());
        c201600000020111A.setB1922(Util.ramdom());
        c201600000020111A.setB1931(Util.ramdom());
        c201600000020111A.setB1932(Util.ramdom());
        c201600000020111A.setB1933(Util.ramdom());
        c201600000020111A.setB201(Util.ramdom().toString());
        c201600000020111A.setB2021(Util.ramdom().toString());
        c201600000020111A.setB2022(Util.ramdom().toString());
        c201600000020111A.setB2031(Util.ramdom().toString());
        c201600000020111A.setB2032(Util.ramdom().toString());
        c201600000020111A.setB2033(Util.ramdom().toString());
        c201600000020111A.setB2034(Util.ramdom().toString());
        c201600000020111A.setB2035(Util.ramdom().toString());
        c201600000020111A.setB2036(Util.ramdom().toString());
        c201600000020111A.setB2037(Util.ramdom().toString());
        c201600000020111A.setB2038(Util.ramdom().toString());
        c201600000020111A.setTjjg(Util.ramdom().toString());
        c201600000020111A.setTjjgjb(Util.ramdom().toString());

    }

    private void addAll(C201600000020111 c201600000020111A) {
        C201600000020111 c201600000020111 = Util.forEachSetValue(new C201600000020111());
        c201600000020111.setBgq(c201600000020111A.getBgq());
        c201600000020111.setZzjgdm(c201600000020111A.getZzjgdm());
        c201600000020111.setDcdxxtm(c201600000020111A.getDcdxxtm());
        BeanUtils.copyProperties(c201600000020111,c201600000020111A);
    }

    private Collection<C201600000020111> selectList(String zzjgdm){
        Map<String ,Object> map =new HashMap<>();
        map.put("zzjgdm",zzjgdm);
        return this.listByMap(map);
    }

}
